<?php
namespace app\parks\controller;
use app\common\controller\HomeCommon;
use think\Request;
use app\parks\model\Parks;
use think\db\Query;
use think\Db;

/**
 * 
 * @author admin
 *
 */
class Userparks extends HomeCommon
{
	public function __construct(){
		parent::__construct();
		$token=input('token');
		//验证token
		$result=$this->check_token($token);
		
		if($result['errcode']!=0){
			
			echo json_encode(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);die;
		}
	}
   
	/**
	 * 我加入的园区
	 */
	public function mypark_list(){
		if(Request::instance()->isPost()){
			$user_id=$this->user_id;
			$list=db('parks')->alias('a')
			                 ->field('a.id,a.park_name,a.logo')
			                 ->join('users_parks b','a.id=b.parkid')
				             ->where(['b.user_id'=>$user_id,'b.status'=>1,'a.status'=>1])
				             ->order('b.id desc')
				             ->paginate();
			 return json(['errcode'=>'0','msg'=>'获取成功！','data'=>$list]);
		}
	}
    /**
     * 加入园区
     */
    public function join_park(){
    	if(Request::instance()->isPost()){
    		$data['parkid']=input('parkid');
    		
    		//验证parkid
    		$result=Parks::check_parks_eg($data['parkid']);
    		if($result==false){
    			return json(['errcode'=>'10002','msg'=>'parkid错误！','data'=>[]]);
    		}
    		
    		
    		$data['user_id']=$this->user_id;
    		
    		//是否已加入
    		$info=db('users_parks')->field('id')->where(['parkid'=>$data['parkid'],'user_id'=>$data['user_id'],'status'=>1])->find();
    		
    		if(!empty($info)){
    			return json(['errcode'=>'10003','msg'=>'你已经加入过该园区！','data'=>[]]);
    		}
    		
    		
    		$result=db('users_parks')->insert($data);
    		if($result){
    			$parks = Parks::get($data['parkid']);
    			$parks->members_num = Db::raw('members_num+1');
    			$parks->save();
    			
    			return json(['errcode'=>'0','msg'=>'加入成功','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10004','msg'=>'加入失败！','data'=>[]]);
    		}
    		
    		
    	}
    }
    /**
     * 退出园区
     */
    public function exit_park(){
    	if(Request::instance()->isPost()){
    		$data['parkid']=input('parkid');
    		
    		//验证parkid
    		$result=Parks::check_parks_eg($data['parkid']);
    		if($result==false){
    			return json(['errcode'=>'10002','msg'=>'parkid错误！','data'=>[]]);
    		}
    		
    		
    		$data['user_id']=$this->user_id;
    		
    		//是否已加入
    		$info=db('users_parks')->field('id')->where(['parkid'=>$data['parkid'],'user_id'=>$data['user_id'],'status'=>1])->find();
    		
    		if(empty($info)){
    			return json(['errcode'=>'10003','msg'=>'你还没有加入过该园区！','data'=>[]]);
    		}
    		
    		$data['id']=$info['id'];
    		$data['status']=0;
    		$result=db('users_parks')->update($data);
    		if($result){
    			$parks = Parks::get($data['parkid']);
    			$parks->members_num = Db::raw('members_num-1');
    			$parks->save();
    			
    			return json(['errcode'=>'0','msg'=>'退出成功','data'=>[]]);
    		}else{
    			return json(['errcode'=>'10004','msg'=>'退出失败！','data'=>[]]);
    		}
    		
    		
    	}
    }
    /**
     * 园区的人员列表
     */
    public function users_list(){
    	
    	if(Request::instance()->isPost()){
	    	$data['parkid']=input('parkid');
	    	
	    	$where=" 1=1 and a.status=1 and b.status=1 ";
	    	if($data['parkid']){
	    		//验证parkid
	    		$result=Parks::check_parks_eg($data['parkid']);
	    		if($result==false){
	    			return json(['errcode'=>'10002','msg'=>'parkid错误！','data'=>[]]);
	    		}else{
	    			$where.=" and b.parkid= ".$data['parkid'];
	    		}
	    	}
	    	
	    	
	    	
	    	$list=db('users_parks')->alias('a')
	    	->field('b.*')
	    	->join('users b','a.user_id=b.id')
	    	->where($where)
	    	->order('b.id desc')
	    	->paginate();
	    	return json(['errcode'=>'0','msg'=>'获取成功！','data'=>$list]);
    	}
    }
}
